import Base from 'models/base';

import moment from 'moment';
import SystemUtil from 'utils/systemUtil';

export default class NoticeForm extends Base {
  constructor(notice) {
    super();

    if (notice === undefined) {
      notice = {
        status: -1
      };
      this.noticeImgUrl = [];
    } else {
      let noticeImgUrl = notice.noticeImgUrl;
      if (SystemUtil.isEmpty(noticeImgUrl)) {
        noticeImgUrl = [];
      } else {
        noticeImgUrl = [noticeImgUrl];
      }
      this.noticeImgUrl = noticeImgUrl;
      this.id = notice.noticeTipId;
      this.createTime = moment(notice.createTime).valueOf();
    }

    let fields = ['nitoceTitle', 'noticeContent', 'noticeTipUrl', 'status'];
    this.initObject(notice, fields);
    this.oldStatus = this.status;
  }

  checkValid() {
    let fields = ['nitoceTitle', 'noticeContent'];
    return this.checkRequired(fields);
  }

  getObject() {
    let postData = {};
    let fields = ['nitoceTitle', 'noticeContent', 'noticeTipUrl', 'status'];
    this.setPostData(postData, fields);

    let noticeImgUrl = this.noticeImgUrl;
    if (noticeImgUrl.length > 0) {
      postData.noticeImgUrl = noticeImgUrl[0];
    }

    let id = this.id;
    if (id !== undefined) {
      postData.noticeTipId = id;
    }
    return postData;
  }
}
